package cn.edu.buaa.txtReader.view;

import cn.edu.buaa.txtReader.DetailActivity;
import util.Global;
import util.PageDao;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class MyView extends View {

    private float startX;
    private float nowX;
    private String content;
    private DetailActivity detail;
    protected String preContent;
    protected String nextContent;

    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        detail = (DetailActivity) context;
        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        this.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction()==MotionEvent.ACTION_DOWN){
                    startX=event.getX();
                }else if (event.getAction()==MotionEvent.ACTION_MOVE){
                    nowX=event.getX();

                    //手指滑动时，调用刷新方法，让文字跟随手指滑动
                    postInvalidate();
                }else if (event.getAction()==MotionEvent.ACTION_UP){
                    if (Math.abs(nowX-startX)>=50){
                        //上一页
                        if (nowX>startX){
                            if (detail.getNowPage()>1){
                                detail.setNowPage(detail.getNowPage()-1);
                                changePage();
                            }
                        }
                        //下一页
                        if (nowX<startX){
                            if (detail.getNowPage()<detail.getPageNum()){
                                detail.setNowPage(detail.getNowPage()+1);
                                changePage();
                            }
                        }
                    }
                    startX=0;
                    nowX=0;
                }
                return false;
            }
        });
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void changePage() {
        content = PageDao.getDataByTxtIdAndNowPage((Integer) detail.getTxtMap().get("id"),detail.getNowPage());
        // 刷新View
        detail.getHandler().sendEmptyMessage(1);
        postInvalidate();

        //准备上一页数据
        if (detail.getNowPage()>1){
            preContent=PageDao.getDataByTxtIdAndNowPage((int)detail.getTxtMap().get("id"),detail.getNowPage()-1);
        }else {
            preContent=null;
        }

        //准备下一页数据
        if (detail.getNowPage()<detail.getPageNum()){
            nextContent=PageDao.getDataByTxtIdAndNowPage((int)detail.getTxtMap().get("id"),detail.getNowPage()+1);
        }else {
            nextContent=null;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //当前页
        if (content!=null) {
            this.flush(canvas,(int)(nowX-startX));
            //上一页
            if (nowX>startX){
                this.flush(canvas,(int)(nowX-startX-Global.SCREEN_WIDTH));
                //下一页
            }else {
                this.flush(canvas,(int)(nowX-startX+Global.SCREEN_WIDTH));
            }
        }

    }

    //对改变x坐标后更新界面的方法进行封装
    public void flush(Canvas canvas,int plus){
        Paint paint = new Paint();
        paint.setTextSize(Global.CHAR_SIZE);
        paint.setColor(Color.WHITE);
        String[] data = content.split(Global.LINE_OVER_FLAG);
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length(); j++) {
                if (j+i*Global.LINE_CHAR_COUNT<content.length()) {
                    canvas.drawText(String.valueOf(data[i].charAt(j)),
                            Global.PAGE_SEP + j * (Global.CHAR_SIZE + Global.CHAR_SEP)+plus,
                            (i + 1) * (Global.CHAR_SIZE + Global.LINE_SEP),
                            paint);
                }
            }
        }
    }
}
